<?php
namespace Home\Model;
use Think\Model;
class SayModel extends Model {

    public function check_auth($id,$table){
        $model = M($table);
        $res = $model->field('uid')->find($id);
        $uid = $_SESSION['qzone']['home']['id'];

        if($res['uid'] && $res['uid']==$uid){
            return true;
        } else {
            return false;
        }
    }

    public function com_auth($id){
        $model = M('Say');
        $com_model = M('Say_comment');
        $res1 = $com_model->field('who_id,uid')->find($id);
        $res2 = $model->field('uid')->find($res1['who_id']);
        $uid = $_SESSION['qzone']['home']['id'];

        if($res1['uid']==$uid || $res2['uid']==$uid){
            return true;
        } else {
            return false;
        }
    }
    public function com_hf_auth($id){
        $model = M('Say');
        $com_model = M('Say_comment');
        $com_hf_model = M('Say_comment_huif');
        $res1 = $com_hf_model->field('com_id,uid')->find($id);
        $res2 = $com_model->field('who_id,uid')->find($res1['com_id']);
        $res3 = $model->field('uid')->find($res2['who_id']);
        $uid = $_SESSION['qzone']['home']['id'];

        if($res1['uid']==$uid || $res2['uid']==$uid || $res3['uid']==$uid){
            return true;
        } else {
            return false;
        }
    }

    public function getlist($uid){
      $model = M('say'); 
      $goodModel = M('say_good');
      $photo_model = M('say_photo');
      $say_comment_model = M('say_comment'); 
      $say_comment_huif_model = M('say_comment_huif'); 
      $user_model = M('user');
      
      $count = $model->where(array('uid'=>$uid,'state'=>0))->count();// 查询满足要求的总记录数
      $Page = new \Think\Page($count,10);
      $show = $Page->show();
      //获取用户说说列表
      $list = $model->where(array('uid'=>$uid,'state'=>0))->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();

      foreach($list as $k => $v){
          $pic = $photo_model->where(array('say_id'=>$v['id']))->field('path')->select();
          $goodnum = $goodModel->where(array('say_id'=>$v['id']))->count();
          $is_fav = $this->favcount($v['id']);
          foreach($pic as $k1=>$v1){
              $list[$k]['pic'][] = $v1['path'];
          }

          $list[$k]['fav'] = $goodnum;
          $list[$k]['is_fav'] = $is_fav;
          //获取每条说说的评论列表
          $say_comment_list = $say_comment_model->where(array('who_id'=>$v['id']))->order('ctime asc,id')->select();
          //获取每条评论的用户信息,评论回复信息
          foreach ($say_comment_list as $k2 => $v2) {
              $uid = $v2['uid'];
              $user = $user_model->find($uid);
              //每条评论的用户信息
              $say_comment_list[$k2]['user'] = $user;
              //获取每条评论的回复列表
              $say_com_hflist = $say_comment_huif_model->where(array('com_id'=>$v2['id']))->select();
              foreach ($say_com_hflist as $k3 => $v3) {
                //每条回复的用户信息
                $user1 = $user_model->find($v3['uid']);
                $user2 = $user_model->find($v3['hf_id']);
                $say_com_hflist[$k3]['user1'] = $user1;
                $say_com_hflist[$k3]['user2'] = $user2;
              }
              $say_comment_list[$k2]['hflist'] = $say_com_hflist;
          }
          $list[$k]['comment'] = $say_comment_list;

      }

      $return['data'] = $list;
      $return['page'] = $show;
      $return['say_count'] = $count;
      return $return;
    }

    public function favcount($say_id){
      $model = M('say_good');
      $data['say_id'] = $say_id;
      $data['uid'] = $_SESSION['qzone']['home']['id'];

      $count = $model->where($data)->count();
      return $count;
    }
    
}